package com.niuwa.p2p.core.mvc.entity.security;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetails;

import com.niuwa.p2p.service.iml.springsecurity.MyUserDetailServiceImpl;

public class NiuwaAuthenticationProvider implements AuthenticationProvider {

	@Autowired
	private MyUserDetailServiceImpl userDetailsService;

	@Override
	public Authentication authenticate(Authentication authentication)
			throws AuthenticationException {
		UserDetails userDetails = userDetailsService
				.loadUserByUsername(authentication.getPrincipal().toString());
		UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
				userDetails, authentication.getCredentials(),
				userDetails.getAuthorities());
		token.setDetails(userDetails);
		return token;
	}

	@Override
	public boolean supports(Class<? extends Object> arg0) {
		return true;
	}

	public MyUserDetailServiceImpl getUserDetailsService() {
		return userDetailsService;
	}

	public void setUserDetailsService(MyUserDetailServiceImpl userDetailsService) {
		this.userDetailsService = userDetailsService;
	}

	
	
}
